package com.sundablog.service.impl.systemsetup.permissionsManage.role;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundablog.mapper.AdminRoleMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.mapper.AdminUserRoleMapper;
import com.sundablog.pojo.AdminRole;
import com.sundablog.pojo.AdminRoleExample;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.pojo.AdminUserRoleExample;
import com.sundablog.pojo.AdminRoleExample.Criteria;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.systemsetup.permissionsManage.role.RoleService;

import cn.hutool.core.util.StrUtil;

/**
 * 角色接口实现类
 * @ClassName:  RoleServiceImpl   
 * @Description:角色接口实现类  
 * @author: 哒哒 
 * @date:   2018年1月31日 下午2:10:46   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private AdminRoleMapper adminRoleMapper;
	
	@Autowired
	private AdminRolePermissionMapper adminRolePermissionMapper;
	
	@Autowired
	private AdminUserRoleMapper adminUserRoleMapper;
	
	/**
	 * 分页查询
	 * Title: findPagingQuery   
	 * Description:    
	 * @param page
	 * @param limit
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.role.RoleService#findPagingQuery(int, int)
	 */
	@Override
	public LayUiResult findPagingQuery(int page, int limit) {
		PageHelper.startPage(page, limit);
		AdminRoleExample adminRoleExample = new AdminRoleExample();
		List<AdminRole> list = adminRoleMapper.selectByExample(adminRoleExample);
		PageInfo<AdminRole> pageInfo = new PageInfo<>(list);
		LayUiResult layUiResult = new LayUiResult();
		layUiResult.setCode(0);
		layUiResult.setMsg("");
		layUiResult.setCount(pageInfo.getTotal());
		layUiResult.setData(list);
		return layUiResult;
	}

	/**
	 * 添加一条角色
	 * Title: addRole   
	 * Description:    
	 * @param adminRole
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.role.RoleService#addRole(com.sundablog.pojo.AdminRole)
	 */
	@Override
	public int addRole(AdminRole adminRole,String zTreeResults) {
		
		
		
		
		
		
		/**
		 * 查询数据库有没有此条信息
		 */
		AdminRoleExample example = new AdminRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(adminRole.getName());
		List<AdminRole> list = adminRoleMapper.selectByExample(example);		
		if (list.size() > 0) {
			return 0;
		} else {
			adminRole.setCtime(new Date());
			int i = adminRoleMapper.insert(adminRole);
			List<AdminRole> adminRoles = adminRoleMapper.selectByExample(example);
			AdminRole role = adminRoles.get(0);
			//角色权限添加
			List<String> zTrees = StrUtil.splitTrim(zTreeResults,",");
			for (String string : zTrees) {
				AdminRolePermission adminRolePermission = new AdminRolePermission();
				adminRolePermission.setRoleId(role.getRoleId());
				adminRolePermission.setPermissionId(Integer.valueOf(string));
				int j = adminRolePermissionMapper.insert(adminRolePermission);
				if (j < 0) {
					return 0;
				}
			}
			return i;	
		}
	}

	/**
	 * 根据id查询一条数据
	 * Title: selectAdminRoleById   
	 * Description:    
	 * @param id
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.role.RoleService#selectAdminRoleById(int)
	 */
	@Override
	public AdminRole selectAdminRoleById(int id) {
		AdminRole adminRole = adminRoleMapper.selectByPrimaryKey(id);
		return adminRole;
	}
	/**
	 * 修改一条数据
	 * Title: updataAdminRole   
	 * Description:    
	 * @param adminRole
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.role.RoleService#updataAdminRole(com.sundablog.pojo.AdminRole)
	 */
	@Override
	public int updataAdminRole(AdminRole adminRole,String zTreeResults) {
		int i = adminRoleMapper.updateByPrimaryKeySelective(adminRole);
		/**
		 * 删除全部关于角色的权限，在重新添加
		 */
		AdminRolePermissionExample example = new AdminRolePermissionExample();
		com.sundablog.pojo.AdminRolePermissionExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(adminRole.getRoleId());
		int j = adminRolePermissionMapper.deleteByExample(example);
		if (j < 0) {
			return 0;
		}
		
		
		List<String> zTrees = StrUtil.splitTrim(zTreeResults,",");
		for (String string : zTrees) {
			AdminRolePermission adminRolePermission = new AdminRolePermission();
			adminRolePermission.setRoleId(adminRole.getRoleId());
			adminRolePermission.setPermissionId(Integer.valueOf(string));
			int k = adminRolePermissionMapper.insert(adminRolePermission);
			if (k < 0) {
				return 0;
			}
		}
		
		
		return 1;
	}
	/**
	 * 根据id删除一条数据
	 * Title: delectAdminRoleById   
	 * Description:    
	 * @param id
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.role.RoleService#delectAdminRoleById(int)
	 */
	@Override
	public int delectAdminRoleById(int id) {
		int i = adminRoleMapper.deleteByPrimaryKey(id);
		/**
		 * TODO(角色权限删除没有完成)
		 */
		if (i > 0) {
			AdminRolePermissionExample example = new AdminRolePermissionExample();
			com.sundablog.pojo.AdminRolePermissionExample.Criteria criteria = example.createCriteria();
			criteria.andRoleIdEqualTo(id);
			int j = adminRolePermissionMapper.deleteByExample(example);
			return j;
		} else {
			return 0;
		}
		
		
		
	}

	/**
	 * 查询全部角色
	 * Title: selectRole   
	 * Description:    
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.role.RoleService#selectRole()
	 */
	@Override
	public List<AdminRole> selectRole() {
		AdminRoleExample adminRoleExample = new AdminRoleExample();
		List<AdminRole> list = adminRoleMapper.selectByExample(adminRoleExample);
		return list;
	}

	/**
	 * 根据用户id 查询用户角色关联表数据
	 * Title: selectByUserId   
	 * Description:    
	 * @param userId
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.role.RoleService#selectByUserId(int)
	 */
	@Override
	public AdminUserRole selectByUserId(int userId) {
		AdminUserRoleExample example = new AdminUserRoleExample();
		com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<AdminUserRole> list = adminUserRoleMapper.selectByExample(example);
		return list.get(0);
	}

	@Override
	public List<AdminUserRole> selectByRoleId(int roleId) {
		AdminUserRoleExample example = new AdminUserRoleExample();
		com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<AdminUserRole> list = adminUserRoleMapper.selectByExample(example);
		return list;
	}

	

}
